#include "loginwidget.h"

#include <QGridLayout>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>

#include "toast.h"
#include "phoneloginwidget.h"
#include "verifycodewidget.h"
#include "model/datacenter.h"
#include "mainwidget.h"

LoginWidget::LoginWidget(QWidget *parent)
    : QWidget{parent}
{
    // 1. 设置窗口本身属性
    this->setFixedSize(318, 447);
    this->setWindowTitle("VQ");
    this->setWindowIcon(QIcon(":/resource/image/logo.png"));
    this->setStyleSheet("QWidget { background-color: rgb(255, 255, 255); }");
    this->setAttribute(Qt::WA_DeleteOnClose);

    // 2. 布局管理器
    QGridLayout* layout = new QGridLayout();
    layout->setHorizontalSpacing(0);
    layout->setVerticalSpacing(5);
    layout->setContentsMargins(30, 42, 30, 0);
    this->setLayout(layout);

    // 3. 创建标题头像

    QPushButton* titleBtn = new QPushButton();
    titleBtn->setFixedSize(90, 90);
    titleBtn->setIconSize(QSize(90, 90));
    titleBtn->setIcon(QIcon(":/resource/image/Icon_logo.png"));
    titleBtn->setStyleSheet("QPushButton { border:none; }");

    // 4. 创建用户名输入框
    usernameEdit = new CustomLineEdit();
    usernameEdit->setPlaceholderText("输入用户名");
    QString editStyle = "QLineEdit { border: none; border-radius: 10px; background-color: rgb(240, 240, 240); "
                        "font-size: 15px; padding-left: 5px; }";
    usernameEdit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    usernameEdit->setFixedHeight(43);
    usernameEdit->setStyleSheet(editStyle);


    // 5. 创建密码输入框
    passwordEdit = new CustomLineEdit();
    passwordEdit->setFixedHeight(43);
    passwordEdit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    passwordEdit->setPlaceholderText("输入VQ密码");
    passwordEdit->setStyleSheet(editStyle);
    passwordEdit->setEchoMode(QLineEdit::Password);

    // 6. 创建验证码输入框
    verifyCodeEdit = new CustomLineEdit();
    verifyCodeEdit->setFixedHeight(40);
    verifyCodeEdit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    verifyCodeEdit->setPlaceholderText("输入验证码");
    verifyCodeEdit->setStyleSheet(editStyle);

    // 7. api
    // QPushButton* verifyCodeWidget = new QPushButton();
    // verifyCodeWidget->setText("验证码");
    // verifyCodeWidget->setStyleSheet("QWidget { border: none; }");
    verifyCodeWidget = new VerifyCodeWidget();

    // 8. 创建登录按钮
    submitBtn = new QPushButton();
    submitBtn->setText("登录");
    submitBtn->setFixedHeight(40);
    submitBtn->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    QString btnGreenStyle = "QPushButton { border: none; border-radius: 10px; "
                            "background-color: rgb(7, 193, 96); color: rgb(255, 255, 255);}";
    btnGreenStyle += "QPushButton:hover { background-color: rgb(56, 205, 127); }"
                    "QPushButton:pressed { background-color: rgb(6, 173, 86); }";
    submitBtn->setStyleSheet(btnGreenStyle);


    // 9. 创建切换到手机号登录按钮
    phoneModeBtn = new QPushButton();
    phoneModeBtn->setText("手机号登录");
    phoneModeBtn->setFixedSize(100, 40);
    QString btnWhiteStyle = "QPushButton { border: none; border-radius: 10px; "
                            "background-color: transparent; color: rgb(45, 119, 229); }";
    btnWhiteStyle += "QPushButton:pressed { background-color: rgb(240, 240, 240); }";
    phoneModeBtn->setStyleSheet(btnWhiteStyle);

    // 10. 创建切换模式
    switchModeBtn = new QPushButton();
    switchModeBtn->setFixedSize(100, 40);
    switchModeBtn->setText("注册");
    switchModeBtn->setStyleSheet(btnWhiteStyle);

    // 11. 添加到布局管理器中
    layout->addWidget(titleBtn, 0, 1, 1, 1);
    layout->addWidget(usernameEdit, 1, 0, 1, 5);
    layout->addWidget(passwordEdit, 2, 0, 1, 5);
    layout->addWidget(verifyCodeEdit, 3, 0, 1, 4);
    layout->addWidget(verifyCodeWidget, 3, 4, 1, 1);
    layout->addWidget(submitBtn, 4, 0, 1, 5);
    layout->addWidget(phoneModeBtn, 5, 0, 1, 1);
    layout->addWidget(switchModeBtn, 5, 4, 1, 1);

    // 12. 处理信号槽
    connect(switchModeBtn, &QPushButton::clicked, this, &LoginWidget::switchMode);

    connect(phoneModeBtn, &QPushButton::clicked, this, [=](){
        PhoneLoginWidget* phoneLoginWidget = new PhoneLoginWidget();
        phoneLoginWidget->show();

        this->close();
    });

    connect(submitBtn, &QPushButton::clicked, this, &LoginWidget::clickSubmitBtn);
}

// 切换登录注册模式
void LoginWidget::switchMode()
{
    if (isLoginMode)
    {
        // 当前是登录模式 切换到注册模式
        this->setWindowTitle("注册");
        submitBtn->setText("注册");
        phoneModeBtn->setText("手机号注册");
        switchModeBtn->setText("登录");
    }
    else
    {
        // 当前是注册模式 切换到登录模式
        this->setWindowTitle("登录");
        submitBtn->setText("登录");
        phoneModeBtn->setText("手机号登录");
        switchModeBtn->setText("注册");
    }
    isLoginMode = !isLoginMode;
}

void LoginWidget::clickSubmitBtn()
{
    // 1. 先从输入框拿到必要的内容
    const QString& username = usernameEdit->text();
    const QString& password = passwordEdit->text();
    const QString& verifyCode = verifyCodeEdit->text();
    if (username.isEmpty() || password.isEmpty() || verifyCode.isEmpty())
    {
        Toast::showMessage("用户名/密码/验证码为空!");
        return;
    }
    // 2. 对比验证码
    if (!verifyCodeWidget->checkVerifyCode(verifyCode))
    {
        Toast::showMessage("验证码错误!");
        return;
    }

    // 3. 发送网络请求
    DataCenter* dataCenter = DataCenter::getInstance();
    if (isLoginMode)
    {
        // 登录
        connect(dataCenter, &DataCenter::userLoginDone, this, &LoginWidget::userLoginDone);
        dataCenter->userLoginAsync(username, password);
    }
    else
    {
        // 注册
        connect(dataCenter, &DataCenter::userRegisterDone, this, &LoginWidget::userRegisterDone);
        dataCenter->userRegisterAsync(username, password);
    }
}

void LoginWidget::userLoginDone(bool ok, const QString& reason)
{
    // 检验登录成功与否
    if (!ok)
    {
        Toast::showMessage("登录失败!" + reason);
        return;
    }

    // 登录成功
    MainWidget* mainWidget = MainWidget::getInstance();
    mainWidget->show();

    this->close();
}

void LoginWidget::userRegisterDone(bool ok, const QString &reason)
{
    // 检验注册成功与否
    if (!ok)
    {
        Toast::showMessage("注册失败!" + reason);
        return;
    }
    Toast::showMessage("注册成功!");

    // 切换登录界面
    this->switchMode();

    // 清空输入框
    verifyCodeEdit->clear();

    // 更新验证码
    verifyCodeWidget->refreshVerifyCode();
}

